<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
      title="加入课程"
    >
      <el-row :gutter="15">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-col :span="22">
            <el-form-item label="邀请码" prop="invitationCode">
              <el-input
                v-model="formData.invitationCode"
                placeholder="请输入邀请码邀请码"
                :maxlength="20"
                show-word-limit
                clearable
                prefix-icon="el-icon-help"
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1">
            <el-form-item label prop="sno">
              <el-input type="hidden" v-model="formData.sno" clearable :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { joinClazz } from "@/api/clazz";
export default {
  inheritAttrs: false,
  components: {},
  props: [],
  data() {
    return {
      formData: {
        invitationCode: null,
        sno: this.$store.state.user.id
      },
      rules: {
        invitationCode: [
          {
            required: true,
            message: "请输入邀请码邀请码",
            trigger: "blur"
          },
        ],
        sno: [
          {
            required: true,
            message: "",
            trigger: "blur"
          }
        ]
      }
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs["elForm"].resetFields();
    },
    close() {
      this.$emit("update:visible", false);
    },
    handelConfirm() {
      this.$refs["elForm"].validate(valid => {
        if (!valid) return;
        else {
            this.post();
        }
      });
    },
    async post() {
      const { data: res } = await joinClazz(
        this.formData.invitationCode,
        this.formData.sno
      );
      if (res.code == 20000) {
        this.$notify.success({
          title: res.message,
          message: "加入课程成功"
        });
        await this.$emit("reload");
        // 关闭
        this.close();
      } else {
        return this.$message.error("加入课程失败:" + res.message);
      }
    }
  }
};
</script>

<style scpoed>
</style>
<style style="scss" scpoed>
</style>
